#ifndef VOIP_RELAY_THREAD_H_
#define VOIP_RELAY_THREAD_H_

//author : shike@outlook.com
//  date : 2013/07/05
// brief : cctalk voip engine relay thread

#include "ct_udp_socket.h"
#include "ct_udp_msg_buf.h"
#include "ct_thread.h"
#include "ct_voip_lost_checker_mgr.h"
#include "ct_voip_msg_cache_mgr.h"

namespace AudioCmdIn {
        struct stRelayAudioData;
}
class ctVOIPThreadPool;
class ctVOIPRelayThread:
        public ctThread
{
        public:
                ctVOIPRelayThread(ctVOIPThreadPool *pool, const WORD &id, const eUDPProtoType &type);
                ~ctVOIPRelayThread();
                bool init(const std::string &ip);
                bool connectToRelayServer(const std::string &serverip, const WORD &serverport);
                void setResetData() { reset_data_ = true; }

        private:
                void run();
                void recvData();
                void sendData();
                DWORD cacheSendPacket(const AudioCmdIn::stRelayAudioData* packet, DWORD length);
                void checkPacketLost(AudioCmdIn::stRelayAudioData* rev, DWORD length);
                void copyDataToShare();
                void copyDataFromShare();
                void resetData();

        private:
                ctVOIPThreadPool *pool_;
                WORD id_;
                eUDPProtoType type_;

                struct sockaddr_in server_addr_;
                ctUDPSocket udp_socket_;

                t_BufferCmdQueue snd_queue_;
                t_BufferCmdQueue rcv_queue_;

                bool reset_data_;

                // packet lost checker for: client->server packets.
                ctVOIPLostCheckerMgr    audio_lost_checker_;
                ctVOIPLostCheckerMgr    video_lost_checker_;

                //cache for: server->relayserver packets.
                ctVOIPMsgCacheMgr       audio_send_cache_;
                ctVOIPMsgCacheMgr       video_send_cache_;
};

#endif //VOIP_RS_THREAD_H_
